package com.lfy.fyadmin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfy.fyadmin.common.R;
import com.lfy.fyadmin.entity.RolePermission;
import com.lfy.fyadmin.service.RolePermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role-permission")
@Tag(name = "角色权限关联管理", description = "角色与权限关联关系接口")
public class RolePermissionController {

    private final RolePermissionService rolePermissionService;

    public RolePermissionController(RolePermissionService rolePermissionService) {
        this.rolePermissionService = rolePermissionService;
    }

    @PostMapping
    @Operation(summary = "新增角色权限关联")
    public R<Boolean> save(@RequestBody RolePermission rolePermission) {
        boolean result = rolePermissionService.save(rolePermission);
        return result ? R.ok(true) : R.fail("新增失败");
    }

    @PostMapping("/batch")
    @Operation(summary = "批量新增角色权限关联")
    public R<Boolean> saveBatch(@RequestBody List<RolePermission> rolePermission) {
        //删除之前这个角色旧版的权限对应关系
        QueryWrapper<RolePermission> roleId = Wrappers.query(RolePermission.class)
                .eq("role_id", rolePermission.get(0).getRoleId());
        rolePermissionService.remove(roleId);

        //保存新的关系
        boolean result = rolePermissionService.saveBatch(rolePermission);
        return result ? R.ok(true) : R.fail("新增失败");
    }

    @PutMapping
    @Operation(summary = "修改角色权限关联")
    public R<Boolean> update(@RequestBody RolePermission rolePermission) {
        boolean result = rolePermissionService.updateSelective(rolePermission);
        return result ? R.ok(true) : R.fail("修改失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色权限关联")
    public R<Boolean> delete(@PathVariable Integer id) {
        boolean result = rolePermissionService.removeById(id);
        return result ? R.ok(true) : R.fail("删除失败");
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询角色权限关联")
    public R<RolePermission> getById(@PathVariable Integer id) {
        RolePermission rolePermission = rolePermissionService.getById(id);
        return rolePermission != null ? R.ok(rolePermission) : R.fail("未找到关联");
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询角色权限关联")
    public R<IPage<RolePermission>> findPage(@RequestParam Integer pageNum,
                                              @RequestParam Integer pageSize,
                                             @RequestParam(required = false) Long roleId) {
        Page<RolePermission> page = new Page<>(pageNum, pageSize);
        return R.ok(rolePermissionService.getRolePermissionWithRoleId(page,roleId));
    }
}